#Makefile behavior
.PHONY:	clean fclean re
.SUFFIXES:
MAKEOVERRIDES=

#programs
CC=			gcc
RM=			rm -f

#output
DIR=		.
OUT=		42sh

#files
CFILES=	alloc.c\
				env0.c\
				env1.c\
				error.c\
				escaping0.c\
				escaping1.c\
				job.c\
				jobmgr.c\
        lexer.c\
				main.c\
        parser.c\
				sh.c\
				term.c\
				token.c\
        token_type.c
OFILES=	$(CFILES:.c=.o)
SLIBS=	list get_next_line
AFILES=	$(addsuffix .a,$(SLIBS))

#flags
CFLAGS=	-Iinclude -W -Wall -O2 -pedantic -ansi
LDFLAGS=-Llib $(addprefix -l,$(SLIBS)) -lncurses

all: $(AFILES) $(OFILES)
	$(CC) -o $(DIR)/$(OUT) $(OFILES) $(LDFLAGS)

debug: CFLAGS+=-g -DDEBUG
debug: fclean all
	

%.a:
	@make -C lib/$(basename $@)_src/ DIR=..

%.o: %.c
	$(CC) -o $(@F) -c $< $(CFLAGS)

clean:
	@$(RM) $(OFILES)
	@echo removed objects

fclean: clean
	@$(foreach i,$(SLIBS),\
		make -C lib/$(i)_src fclean DIR=.. -s --no-print-directory;\
	)
	@echo removed library binaries
	@$(RM) $(DIR)/$(OUT)
	@echo removed $(OUT) executable

re: fclean all
	

